//
//  ClassifyTVCell.m
//  MMLOOK
//
//  Created by dllo on 15/9/29.
//  Copyright © 2015年 武俊娥. All rights reserved.
//

#import "ClassifyTVCell.h"
#import "ClassCollectionViewCell.h"
#import "JudgeConnection.h"
#import "Alert.h"
#import "PresentViewController.h"

@interface ClassifyTVCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionViewFlowLayout *layout;
@property (nonatomic, retain) UICollectionView *collection;

@end

@implementation ClassifyTVCell

- (void)dealloc
{
    Block_release(_sendDic);
    [_layout release];
    [_array release];
    [_collection release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.array = [NSMutableArray array];
              [self creatCollcection];
    }
    return self;
}

- (void)creatCollcection
{
    self.layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collection = [[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout] autorelease];
    
    _collection.dataSource = self;
    _collection.delegate = self;
    _collection.showsHorizontalScrollIndicator = NO;
    _collection.showsVerticalScrollIndicator = NO;
    [_collection registerClass:[ClassCollectionViewCell class] forCellWithReuseIdentifier:@"classify"];
    _collection.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_collection];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.frame.size.width;
    CGFloat height = self.contentView.frame.size.height;
    
    _layout.sectionInset = UIEdgeInsetsMake(0, (width - height * 5 / 1.4) / 6, 0, (width - height * 5 / 1.4) / 6);
    _layout.minimumLineSpacing = (width - height * 5 / 1.4) / 6;
    _layout.itemSize = CGSizeMake(height / 1.4, height);
    _collection.frame = CGRectMake(0, 0, width, height);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"classify" forIndexPath:indexPath];
    
    NSMutableDictionary *dic = [_array objectAtIndex:indexPath.row];
    
    [cell sendDic:dic];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([JudgeConnection judgeConnection]) {
      
        self.sendDic([_array objectAtIndex:indexPath.item]);
    } else {
        
        Alert *alert = [[Alert alloc] initWithFrame:CGRectMake((self.window.frame.size.width - 250) / 2, self.window.frame.size.height * 3 / 4, 250, 50) LabelText:@"当前无网络连接，请检查网络状态"];
        [self.window addSubview:alert];
        [alert release];
    }
}

- (void)sendDicWithBlock:(void (^)(NSMutableDictionary *))block
{
    self.sendDic = block;
}

- (void)setArray:(NSMutableArray *)array
{
    if (_array != array) {
        
        [_array release];
        _array = [array retain];
    }
    
    [_collection reloadData];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
